package com.mebs.edi.datalayer.jpa;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.log4j.Logger;

import com.mebs.edi.datalayer.GenericDao;

public class JpaGenericDao implements GenericDao {
	
	private static final Logger LOG = Logger.getLogger(JpaGenericDao.class);
	private static final String PERSISTENCE_UNIT_NAME = "ediPersistenceUnit";
	private static final EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
	
	protected EntityManager getEntityManager() {
		LOG.debug("getting Entity Manager");
		return emFactory.createEntityManager();
	}
	
	public void save(Object o) {
		EntityManager em = getEntityManager();
		em.getTransaction().begin();
		em.persist(o);
		em.getTransaction().commit();
	}
	
	public void update(Object o) {
		EntityManager em = getEntityManager();
		em.getTransaction().begin();
		em.merge(o);
		em.getTransaction().commit();
	}
}
